package com.luli;

import com.luli.mapper.UserMapper;
import com.luli.pojo.User;
import com.luli.service.IUserService;
import com.luli.service.impl.IUserServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * 陆离
 * 2024/10/11 9:13:53
 */
public class Mytest {
    private SqlSession session;


    @Before
    public void before(){
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void test(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.userList();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void testAdvisors(){
        ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
        IUserService userMapper = app.getBean("users", IUserService.class);
        int i = userMapper.addUser();
        System.out.println(i);
    }

    @Test
    @Transactional
    public void testNoTransaction(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.addUser(new User(7, "root", "password"));
        int a = 10 / 0;
        mapper.updateUser(new User(7,"lzp","234"));
    }


    @After
    public void after(){
        session.commit();
        session.close();
    }
}
